package com.sq.auth.application.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.sq.auth.application.dto.AuthUserDTO;
import com.sq.auth.common.entity.Result;
import com.sq.auth.domain.bo.AuthUserBO;
import com.sq.auth.domain.service.AuthUserDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user/")
@Slf4j
public class AuthUserController {

    @Resource
    private AuthUserDomainService authUserDomainService;

    @PostMapping("register")
    public Result register(@RequestBody AuthUserDTO authUserDTO) {
        if (log.isInfoEnabled()) {
            log.info("AuthUserController.register.dto:{}", JSON.toJSONString(authUserDTO));
        }
        try {
            Preconditions.checkArgument(StrUtil.isNotBlank(authUserDTO.getUserName()), "用户名不能为空");
            Preconditions.checkArgument(StrUtil.isNotBlank(authUserDTO.getPhone()), "手机号不能为空");
            Preconditions.checkArgument(StrUtil.isNotBlank(authUserDTO.getEmail()), "邮箱不能为空");
            Preconditions.checkArgument(StrUtil.isNotBlank(authUserDTO.getPassword()), "密码不能为空");
            Preconditions.checkNotNull(authUserDTO.getSex(), "性别不能为空");
            AuthUserBO authUserBO = BeanUtil.copyProperties(authUserDTO, AuthUserBO.class);
            return Result.ok(authUserDomainService.register(authUserBO));
        } catch (Exception e) {
            log.error("AuthUserController.register.error:{}", e.getMessage());
            return Result.fail("注册用户失败");
        }
    }


    @PostMapping("update")
    public Result update(@RequestBody AuthUserDTO authUserDTO) {
        if (log.isInfoEnabled()) {
            log.info("AuthUserController.update.dto:{}", JSON.toJSONString(authUserDTO));
        }
        try {
            Preconditions.checkNotNull(authUserDTO.getUserName(), "用户名不能为空");
            AuthUserBO authUserBO = BeanUtil.copyProperties(authUserDTO, AuthUserBO.class);
            return Result.ok(authUserDomainService.update(authUserBO));
        } catch (Exception e) {
            log.error("AuthUserController.update.error:{}", e.getMessage());
            return Result.fail("更新用户失败");
        }
    }

    @PostMapping("changeStatus")
    public Result changeStatus(@RequestBody AuthUserDTO authUserDTO) {
        if (log.isInfoEnabled()) {
            log.info("AuthUserController.changeStatus.dto:{}", JSON.toJSONString(authUserDTO));
        }
        try {
            Preconditions.checkNotNull(authUserDTO.getUserName(), "用户名不能为空");
            Preconditions.checkNotNull(authUserDTO.getStatus(), "状态不能为空");
            AuthUserBO authUserBO = BeanUtil.copyProperties(authUserDTO, AuthUserBO.class);
            return Result.ok(authUserDomainService.update(authUserBO));
        } catch (Exception e) {
            log.error("AuthUserController.changeStatus.error:{}", e.getMessage());
            return Result.fail("更新用户状态失败");
        }
    }

    @PostMapping("getUserInfo")
    public Result getUserInfo(@RequestBody AuthUserDTO authUserDTO) {
        if (log.isInfoEnabled()) {
            log.info("AuthUserController.getUserInfo.dto:{}", JSON.toJSONString(authUserDTO));
        }
        try {
            Preconditions.checkNotNull(authUserDTO.getUserName(), "用户名不能为空");
            AuthUserBO authUserBO = BeanUtil.copyProperties(authUserDTO, AuthUserBO.class);
            return Result.ok(authUserDomainService.getUserInfo(authUserBO));
        } catch (Exception e) {
            log.error("AuthUserController.getUserInfo.error:{}", e.getMessage());
            return Result.fail("查询用户信息失败");
        }
    }

    /**
     * 批量获取用户信息
     */
    @RequestMapping("listByIds")
    public Result<List<AuthUserDTO>> listUserInfoByIds(@RequestBody List<String> userNameList) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.listUserInfoByIds.dto:{}", JSON.toJSONString(userNameList));
            }
            Preconditions.checkArgument(!CollUtil.isEmpty(userNameList), "id集合不能为空");
            List<AuthUserBO> userInfos = authUserDomainService.listUserInfoByIds(userNameList);
            return Result.ok(BeanUtil.copyToList(userInfos, AuthUserDTO.class));
        } catch (Exception e) {
            log.error("UserController.listUserInfoByIds.error:{}", e.getMessage(), e);
            return Result.fail("批量获取用户信息失败");
        }
    }


    @PostMapping("delete")
    public Result delete(@RequestBody AuthUserDTO authUserDTO) {
        if (log.isInfoEnabled()) {
            log.info("AuthUserController.delete.dto:{}", JSON.toJSONString(authUserDTO));
        }
        try {
            Preconditions.checkNotNull(authUserDTO.getId(), "用户id为空");
            AuthUserBO authUserBO = BeanUtil.copyProperties(authUserDTO, AuthUserBO.class);
            return Result.ok(authUserDomainService.delete(authUserBO));
        } catch (Exception e) {
            log.error("AuthUserController.delete.error:{}", e.getMessage());
            return Result.fail("删除用户失败");
        }
    }


    @RequestMapping("doLogin")
    public Result doLogin(@RequestParam(value = "validCode") String validCode) {
        if (log.isInfoEnabled()) {
            log.info("AuthUserController.doLogin.validCode:{}", validCode);
        }
        try {
            Preconditions.checkArgument(StrUtil.isNotBlank(validCode), "验证码不能为空");
            SaTokenInfo saTokenInfo = authUserDomainService.doLogin(validCode);
            if(saTokenInfo==null){
                return Result.fail("用户登录失败");
            }
            return Result.ok(saTokenInfo);
        } catch (Exception e) {
            log.error("AuthUserController.doLogin.error:{}", e.getMessage());
            return Result.fail("用户登录失败");
        }
    }

    @GetMapping("logOut")
    public Result logOut(@RequestParam(value = "userName")String userName){
        if (log.isInfoEnabled()) {
            log.info("AuthUserController.doLogin.logOut:{}", userName);
        }
        try {
            Preconditions.checkArgument(StrUtil.isNotBlank(userName), "用户名不能为空");
            StpUtil.logout(userName);
            return Result.ok();
        }catch (Exception e){
            log.error("UserController.logOut.error:{}", e.getMessage(), e);
            return Result.fail("用户登出失败");
        }
    }

    @RequestMapping("isLogin")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }

}
